package com.yunduo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunduo.common.RetunData.CommonReturn;
import com.yunduo.common.RetunData.statuscode.impl.ResultCode;
import com.yunduo.common.RetunData.statuscode.impl.ServiceCode;
import com.yunduo.common.YunDuoConst.IndexConst;
import com.yunduo.entity.Good;
import com.yunduo.entity.GoodType;
import com.yunduo.entity.Log;
import com.yunduo.entity.vo.IndexTitle;
import com.yunduo.service.GoodService;
import com.yunduo.service.GoodTypeService;
import com.yunduo.service.LogService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 首页数据管理
 */
@RequiresRoles("admin")
@RestController
@RequestMapping("/index")
public class IndexController {

    @Resource
    private LogService logService;
    @Resource
    private GoodTypeService goodTypeService;
    @Resource
    private GoodService goodService;

    //首页导航条
    @GetMapping("/titleList")
    public CommonReturn getTitleList(){
        List<String> goodTypeIds = logService.getLogByName(IndexConst.INDEX_LIST.getEName());
        Iterator<String> iterator = goodTypeIds.iterator();
        ArrayList<IndexTitle> list = new ArrayList<>();
        //添加推荐查询数据
        list.add(new IndexTitle(IndexConst.INDEX_PUSH.getEName(),IndexConst.INDEX_PUSH.getName()));
        while (iterator.hasNext()){
            String typeId = iterator.next();
            GoodType type = goodTypeService.getById(typeId);
            list.add(new IndexTitle(type.getId(),type.getType()));
        }
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData(IndexConst.INDEX_LIST.getEName(),list);
    }

    //首页查询数据
    @PostMapping("/getData/{id}")
    public CommonReturn getDataById(@PathVariable("id")String id){
        if(goodTypeService.getById(id) == null){
            return CommonReturn.ERROR(ResultCode.ERROR);
        }
        //查询结点下的所有分类
        ArrayList<String> typeList = new ArrayList<>();
        List<String> listId = getListDataById(typeList,id);
        List<Good> goods = goodService.selectGoodByTypeId(listId);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("list",goods);
    }

    //查询首页导航条数据列表
    @GetMapping("/titles")
    public CommonReturn getTitles(){
        List<String> goodTypeIds = logService.getLogByName(IndexConst.INDEX_LIST.getEName());
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("typeId",goodTypeIds);
    }

    //增加首页导航数据列表
    @GetMapping("/addTitle/{id}")
    public CommonReturn addTitles(@PathVariable("id") String id){
        Log isExist = logService.getLogByEventAndResult(IndexConst.INDEX_LIST.getEName(), id);
        if(isExist!=null){
            return CommonReturn.ERROR(ResultCode.ERROR).setMessage(ServiceCode.IS_EXIST.getInfo());
        }
        GoodType type= goodTypeService.getById(id);
        if(type == null){
            return CommonReturn.ERROR(ResultCode.ERROR).setMessage(ServiceCode.PARAM_ERROR.getInfo());
        }
        Log indexLog = new Log();
        indexLog.setEvent(IndexConst.INDEX_LIST.getEName());
        indexLog.setResult(id);
        logService.save(indexLog);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    //删除首页导航数据列表
    @DeleteMapping("/deleteTitle/{id}")
    public CommonReturn deleteTitles(@PathVariable("id") String id){
        LambdaQueryWrapper<Log> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Log::getEvent,IndexConst.INDEX_LIST.getEName());
        queryWrapper.eq(Log::getResult,id);
        Log indexLog = logService.getOne(queryWrapper);
        logService.removeById(indexLog.getId());
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    //首页店铺推荐添加
    @GetMapping("/addShop/{id}")
    public CommonReturn addShop(@PathVariable("id") String id){
        Log isExist = logService.getLogByEventAndResult(IndexConst.INDEX_PUSH.getEName(), id);
        if(isExist != null){
            return CommonReturn.ERROR(ResultCode.ERROR).setMessage(ServiceCode.IS_EXIST.getInfo());
        }
        Log pushLog = new Log();
        pushLog.setEvent(IndexConst.INDEX_PUSH.getEName());
        pushLog.setResult(id);
        logService.save(pushLog);
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    //首页店铺删除
    @DeleteMapping("/deleteShop/{id}")
    public CommonReturn deleteShop(@PathVariable("id") String id){
        Log indexLog = logService.getLogByEventAndResult(IndexConst.INDEX_PUSH.getEName(), id);
        if(indexLog != null){
            logService.removeById(indexLog.getId());
        }
        return CommonReturn.SUCCESS(ResultCode.SUCCESS);
    }

    //首页店铺查询
    @GetMapping("/getShops")
    public CommonReturn getShops(){
        List<String> shopIds = logService.getLogByName(IndexConst.INDEX_PUSH.getEName());
        return CommonReturn.SUCCESS(ResultCode.SUCCESS).setData("shopIds",shopIds);
    }

    //递归查询该分类下所有分类
    private List<String> getListDataById(List<String> list,String id){
        list.add(id);
        List<GoodType> dataList = goodTypeService.list(new LambdaQueryWrapper<GoodType>().eq(GoodType::getParentId, id));
        dataList.forEach(item->{
            List<GoodType> temp = goodTypeService.list(new LambdaQueryWrapper<GoodType>().eq(GoodType::getParentId, item.getId()));
            if (temp.size() == 0){
                list.add(item.getId());
                return;
            }
            getListDataById(list,item.getId());
        });
        return list;
    }

}
